<?php

namespace App\Models\Site;

use App\Models\BasicModel;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\SiteArticle
 *
 * @property int $id id
 * @property int|null $cate_id 文章栏目
 * @property string|null $title 文章标题
 * @property string|null $summary 文章简述
 * @property string|null $image 图片链接
 * @property string|null $content 文章内容
 * @property int|null $sort 排序权重
 * @property \Illuminate\Support\Carbon|null $created_at 创建时间
 * @property \Illuminate\Support\Carbon|null $updated_at 更新时间
 * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
 * @property int|null $status 状态
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle newQuery()
 * @method static \Illuminate\Database\Query\Builder|SiteArticle onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle query()
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereCateId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereSummary($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SiteArticle whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|SiteArticle withTrashed()
 * @method static \Illuminate\Database\Query\Builder|SiteArticle withoutTrashed()
 * @mixin \Eloquent
 */
class SiteArticle extends BasicModel
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'site_article';

    protected $list_fields = ['id', 'cate_id', 'title', 'image', 'summary', 'sort', 'created_at'];
    protected $fields = ['id', 'cate_id', 'title', 'image', 'summary', 'content', 'sort', 'created_at'];


    /**
     * @param int $cate_id
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getList($cate_id = 0,$uid,$sk)
    {
        $model = self::select($this->list_fields);
        if ($cate_id > 0) $model->where('cate_id', $cate_id);
        if($sk) $model->where('title','like',"%{$sk}%");
        $list = $model->paginate();
        foreach ($list as $item){
            $item->is_read=0;
            if(ArticleReaded::where(['uid'=>$uid,'article_id'=>$item->id])->first()){
                $item->is_read=1;
            }
        }
        return $list;
    }

    /**
     * @param $id
     * @return SiteArticle|\Illuminate\Database\Eloquent\Builder|Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getDetail($id)
    {
        return self::where('id', $id)->select($this->fields)->first();
    }

}
